package net.dgg.zz.production.junctions.controller;

import net.dgg.framework.tac.utils.web.commmon.DggBaseController;
import net.dgg.iboss.base.util.CustomerNumberEncryptionUtil;
import net.dgg.iboss.base.util.DesUtil;
import net.dgg.iboss.base.util.datatable.DataTableUtils;
import net.dgg.iboss.base.util.datatable.domain.DataTableRequest;
import net.dgg.iboss.base.util.datatable.domain.DataTableResponse;
import net.dgg.tmd.foundation.platform.common.util.TranslateUtil;
import net.dgg.tmd.foundation.platform.org.service.OrganizationManager;
import net.dgg.tmd.foundation.platform.session.SessionManager;
import net.dgg.tmd.foundation.platform.user.entity.UserEntity;
import net.dgg.zz.common.constant.ScProductConstant;
import net.dgg.zz.common.service.CommonMangerExtService;
import net.dgg.zz.common.utils.DateUtils;
import net.dgg.zz.common.utils.OrderHandleUtils;
import net.dgg.zz.common.utils.StringUtils;
import net.dgg.zz.common.utils.excel.ExcelUtils;
import net.dgg.zz.production.chargeback.service.ZzAuditOrderServic;
import net.dgg.zz.production.vo.InprocessOrderVo;
import net.dgg.zz.production.vo.JunctionsOrderVo;
import net.dgg.zz.production.vo.OrgOrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 孙上峰
 * @ClassName <ZzJunctionsOrderController>
 * @despriction 结项订单
 * @create 2018/08/20 10:38
 **/
@Controller
@RequestMapping("/zzsc/junctions")
public class ZzJunctionsOrderController extends DggBaseController {

    @Autowired
    protected DesUtil desUtil;
    @Autowired
    protected CommonMangerExtService zzCommonMangerExtService;
    @Autowired
    protected SessionManager sessionManager;
    @Autowired
    private TranslateUtil translateUtil;
    @Autowired
    private ZzAuditOrderServic zzAuditOrderServic;
    @Autowired
    private OrganizationManager organizationManager;

    /**
     * 初始化界面
     */
    @RequestMapping(value = "/index")
    public String index(Model model) {
        return "zzsc/junctions/page_order_junctions";
    }

    /**
     * 结项分页列表
     * 孙上峰
     *
     * @param params {codeName:模块名称}
     * @return
     */
    @RequestMapping(value = "/query_junctions_list")
    @ResponseBody
    public DataTableResponse queryJunctionsList(@RequestParam Map params, HttpServletRequest request) {
        DataTableRequest r = DataTableUtils.getParam(request);
        UserEntity userEntity = sessionManager.getCurrentUser();
        params.put("start", r.getStart());
        params.put("limit", r.getLength());
        params.put("userEntity", userEntity.getId()); //当前登录人auditorId
        params.put("status", ScProductConstant.SC_ORDER_JUNCTIONS);//已结项订单
        if (!net.dgg.iboss.base.util.StringUtils.isEmptyObj(params.get("customerPhone"))) {
            params.put("customerPhone", desUtil.encrypt(params.get("customerPhone").toString()));
        }
        List<Long> orgIds = organizationManager.findOrgIdByAncestorId(userEntity.getOrgId());
        params.put("orgIdList", orgIds);
        if(!StringUtils.isEmpty(params.get("cq_status"))) {
            if (params.get("cq_status").equals("1")) {
                params.put("orderTimeStatus", ScProductConstant.TIME_OUT);
            } else if (params.get("cq_status").equals("2")) {
                params.put("nodeTimeStatus", ScProductConstant.TIME_OUT);
            }
        }
        Map fileMap = new HashMap();
        fileMap.put("signTime", "signTime");
        OrderHandleUtils.getOrderCol(r, params, fileMap);
        List<Map> listPage = zzAuditOrderServic.queryAuditListPage(params);
        String[] transColum = {"originCode", "originType", "status", "businessTypeId", "businessProductId", "abandonReason"};
        listPage = translateUtil.translateList(transColum, listPage);
        String[] hideColum = {"customerPhone"};
        listPage = desUtil.decryptListMap(hideColum, listPage);
        CustomerNumberEncryptionUtil.encryptionList(hideColum, listPage);
        Integer count = Integer.parseInt(params.get("count").toString());
        return new DataTableResponse(r.getDraw(), count, count, listPage, "");
    }

    /**
     * <p>@Description 结项导出</p>
     * <p>@author zhangwen</p>
     * <p>@Time 2018/8/31 0031 16:19 创建时间</p>
     * <p>@return</p>
     */
    @RequestMapping("/export_product_order")
    public void exportSczzAbnormal(@RequestParam Map params, HttpServletResponse response) {
        try {
            String fileName = "结项订单_" + DateUtils.dateString(new Date(), "yyyyMMddHHmm") + ".xlsx";
            UserEntity userEntity = sessionManager.getCurrentUser();
            params.put("status", ScProductConstant.SC_ORDER_JUNCTIONS);//办理中
            params.put("userEntity", userEntity.getId()); //当前登录人
            if (StringUtils.isNotEmpty(params.get("customerPhone"))) {
                params.put("customerPhone", desUtil.encrypt(params.get("customerPhone").toString()));
            }
            if(!StringUtils.isEmpty(params.get("cq_status"))) {
                if (params.get("cq_status").equals("1")) {
                    params.put("orderTimeStatus", ScProductConstant.TIME_OUT);
                } else if (params.get("cq_status").equals("2")) {
                    params.put("nodeTimeStatus", ScProductConstant.TIME_OUT);
                }
            }
            //获取判断界面展示内容的key
            getAcquisitionConditions(params);
            List<JunctionsOrderVo> dataList = zzAuditOrderServic.findExportProductOrder(params);
            ExcelUtils.writeToFile(dataList, null, fileName, response,JunctionsOrderVo.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * <p>@Description 获取判断界面展示内容的key</p>
     * <p>@author mqy</p>
     * <p>@Time 2018/8/21 0027 15:20 创建时间</p>
     * <p>@return</p>
     */
    private void getAcquisitionConditions(Map<String, Object> map) {
        String type = null == map.get("type") ? "" : map.get("type").toString();
        if ("toDay".equals(type)) { //今日待跟进
            map.put("toDay", "toDay");
        } else if ("currentDay".equals(type)) { //新增办理
            map.put("currentDay", "currentDay");
        } else if ("hasOverDue".equals(type)) { //超期跟进
            map.put("hasOverDue", "hasOverDue");
        } else if ("nodeOverDue".equals(type)) { //节点超期
            map.put("nodeOverDue", ScProductConstant.TIME_OUT);
        } else if ("proOverDue".equals(type)) { //订单超期
            map.put("proOverDue", ScProductConstant.TIME_OUT);
        } else {
            map.put("all", ScProductConstant.SC_ORDER_IN_PROCESS); //全部
        }
    }
}
